use "similarity_wellbeing.dta", clear

* Setting covariates for the analyses
	global covariates ///
		mom_logearnings mom_logearnings_m dad_logearnings dad_logearnings_m i.birthyear ///
		female female_m mom_highschool mom_short mom_vocation mom_medium mom_long mom_edu_m ///
		dad_highschool dad_short dad_vocation dad_medium dad_long dad_edu_m single_mom single_mom_m concen_non_danish_a9010 non_western non_danish

	global covariates_no_mom ///
		dad_logearnings dad_logearnings_m i.birthyear ///
		female female_m ///
		dad_highschool dad_short dad_vocation dad_medium dad_long dad_edu_m 
		
	global classcovar ///
		class_mom_logearnings class_mom_logearnings_m class_dad_logearnings ///
		class_dad_logearnings_m  class_female class_female_m ///
		class_mom_highschool class_mom_short class_mom_vocation class_mom_medium ///
		class_mom_long class_mom_edu_m class_dad_highschool class_dad_short ///
		class_dad_vocation class_dad_medium class_dad_long class_dad_edu_m ///
		class_single_mom class_single_mom_m

	
* Figure 2 (sub-group effects)

	// Panel A (non-Western)
		global sample  if classroom_n<30 & classroom_n>10 &  ingroup!=. & mom_id!=. & western==0 // Define sample such that western minority students are dropped from the analyses and with the same sample across models
	
		reg wellbeing c.concen_non_west_a9010##i.non_western i.grade i.school_year $covariates $classcovar $sample ,  vce(cluster class_id) // Baseline
			
		reghdfe wellbeing c.concen_non_west_a9010##i.non_western  i.grade i.school_year $covariates $classcovar $sample , absorb(school_id) vce(cluster class_id) keepsingletons // School FE
			
		reghdfe wellbeing c.concen_non_west_a9010##i.non_western  i.grade i.school_year $covariates_no_mom $classcovar $sample , absorb(mom_id) vce(cluster class_id) keepsingletons // Family FE
			
		reghdfe wellbeing c.concen_non_west_a9010##i.non_western  i.grade i.school_year  $classcovar $sample , absorb(person_id) vce(cluster class_id) keepsingletons // Student FE
		
		reghdfe wellbeing c.concen_non_west_a9010##i.non_western  i.grade i.school_year  $classcovar $sample , absorb(person_id school_id) vce(cluster class_id) keepsingletons // Combined FE
	
	
	// Panels B, C, D (non-native)
		global sample  if classroom_n<30 & classroom_n>10 &  ingroup!=. & mom_id!=. // Baseline sample with the same sample across models
		
		foreach var of varlist concen_non_danish_a9010 ingroup_reg_a9010 ingroup_a9010 { // Run for each of the three concentration measures: Share of non-native, co-regional, and co-ethnic students
		
		reg wellbeing c.`var'##i.non_danish i.grade i.school_year $covariates $classcovar $sample ,  vce(cluster class_id) // Baseline
			
		reghdfe wellbeing c.`var'##i.non_danish  i.grade i.school_year $covariates $classcovar $sample , absorb(school_id) vce(cluster class_id) keepsingletons // School FE
			
		reghdfe wellbeing c.`var'##i.non_danish  i.grade i.school_year $covariates_no_mom $classcovar $sample , absorb(mom_id) vce(cluster class_id) keepsingletons // Family FE
			
		reghdfe wellbeing c.`var'##i.non_danish  i.grade i.school_year  $classcovar $sample , absorb(person_id) vce(cluster class_id) keepsingletons // Student FE
		
		reghdfe wellbeing c.`var'##i.non_danish  i.grade i.school_year  $classcovar $sample , absorb(person_id school_id) vce(cluster class_id) keepsingletons // Combined FE
		
		}


* Figure 3 (interaction effects)
	// Panel A (non-Western)
		global sample  if classroom_n<30 & classroom_n>10 &  ingroup!=. & mom_id!=. & western==0 // Define sample such that western minority students are dropped from the analyses
	
		reg wellbeing c.concen_non_west##i.non_western i.grade i.school_year $covariates $classcovar $sample ,  vce(cluster class_id) // Baseline
			
		reghdfe wellbeing c.concen_non_west##i.non_western  i.grade i.school_year $covariates $classcovar $sample , absorb(school_id) vce(cluster class_id) keepsingletons // School FE
			
		reghdfe wellbeing c.concen_non_west##i.non_western  i.grade i.school_year $covariates_no_mom $classcovar $sample , absorb(mom_id) vce(cluster class_id) keepsingletons // Family FE
			
		reghdfe wellbeing c.concen_non_west##i.non_western  i.grade i.school_year  $classcovar $sample , absorb(person_id) vce(cluster class_id) keepsingletons // Student FE
		
		reghdfe wellbeing c.concen_non_west##i.non_western  i.grade i.school_year  $classcovar $sample , absorb(person_id school_id) vce(cluster class_id) keepsingletons // Combined FE
	
	
	// Panels B, C, D (non-native)
		global sample  if classroom_n<30 & classroom_n>10 &  ingroup!=. & mom_id!=. // Baseline sample
		
		foreach var of varlist concen_non_danish ingroup_reg ingroup { // Run for each of the three concentration measures: Share of non-native, co-regional, and co-ethnic students
		
		reg wellbeing c.`var'##i.non_danish i.grade i.school_year $covariates $classcovar $sample ,  vce(cluster class_id) // Baseline
			
		reghdfe wellbeing c.`var'##i.non_danish  i.grade i.school_year $covariates $classcovar $sample , absorb(school_id) vce(cluster class_id) keepsingletons // School FE
			
		reghdfe wellbeing c.`var'##i.non_danish  i.grade i.school_year $covariates_no_mom $classcovar $sample , absorb(mom_id) vce(cluster class_id) keepsingletons // Family FE
			
		reghdfe wellbeing c.`var'##i.non_danish  i.grade i.school_year  $classcovar $sample , absorb(person_id) vce(cluster class_id) keepsingletons // Student FE
		
		reghdfe wellbeing c.`var'##i.non_danish  i.grade i.school_year  $classcovar $sample , absorb(person_id school_id) vce(cluster class_id) keepsingletons // Combined FE
		
		}


* Figure 4
	global sample  if classroom_n<30 & classroom_n>10 &  ingroup!=. & mom_id!=. & western==0 // Define sample such that western minority students are dropped from the analyses and with the same sample across models

	// Panel A
		reghdfe wellbeing i.concentration_non_western_cat##non_western i.grade i.school_year $covariates $classcovar $sample , absorb(school_id) vce(cluster class_id)  keepsingletons

	// Panel B (histogram)
		sort school_year school_id grade class_id
		egen tagclass=tag(class_id)
		global sample  if classroom_n<30 & classroom_n>10 &  ingroup!=. & mom_id!=. & western==0 // Define sample such that western minority students are dropped from the analyses
		histogram concentration_non_western  $sample & tagclass==1 & wellbeing!=., percent xlabel(0(0.1)1) scheme(s1mono) ylabel(0(10)50) name(non_western_hist, replace)  legend(off)

	// Panel C (residual variation)
		global sample  if classroom_n<30 & classroom_n>10 &  ingroup!=. & mom_id!=. & western==0 // Define sample such that western minority students are dropped from the analyses
		bys school_id: egen schavg_concentration_non_western = mean(concentration_non_western) $sample & tagclass==1 & wellbeing!=.
		gen schdev_concentration_non_western = concentration_non_western-schavg_concentration_non_western $sample & tagclass==1 & wellbeing!=.
		summ schdev_concentration_non_western $sample & tagclass==1 & wellbeing!=.
		hist schdev_concentration_non_western $sample & tagclass==1 & wellbeing!=., percent scheme(s1mono) legend(off) xtitle("Deviations from school average") xscale(range(-1 (0.5) 1)) text(25 0.75 "SD = 0.066") yscale(range(0 (10) 50)) ylab(#6) xlab(#5)



* Figure 5
	
	// Run regression for each item in the well-being index
		global sample  if classroom_n<30 & classroom_n>10 &  ingroup!=. & mom_id!=. & western==0 // Define sample such that western minority students are dropped from the analyses and with the same sample across models
		foreach var of varlist wellbeing q1 q2 q9 q13 q33 q35 q36 {
			eststo: reghdfe `var' i.concentration_non_western_cat##non_western i.grade i.school_year $covariates $classcovar $sample , absorb(school_id) vce(cluster class_id)  keepsingletons
			}



* Figure 6
	global sample  if classroom_n<30 & classroom_n>10 &  ingroup!=. & mom_id!=. // Baseline sample with the same sample across models

	reg wellbeing c.mena_a9010##i.mena_groups i.grade i.school_year $covariates $classcovar $sample ,  vce(cluster class_id) // Baseline
		
	reghdfe wellbeing c.mena_a9010##i.mena_groups  i.grade i.school_year $covariates $classcovar $sample , absorb(school_id) vce(cluster class_id) keepsingletons // School FE
		
	reghdfe wellbeing c.mena_a9010##i.mena_groups  i.grade i.school_year $covariates_no_mom $classcovar $sample , absorb(mom_id) vce(cluster class_id) keepsingletons // Family FE
		
	reghdfe wellbeing c.mena_a9010##i.mena_groups  i.grade i.school_year  $classcovar $sample , absorb(person_id) vce(cluster class_id) keepsingletons // Student FE
		
	reghdfe wellbeing c.mena_a9010##i.mena_groups  i.grade i.school_year  $classcovar $sample , absorb(person_id school_id) vce(cluster class_id) keepsingletons // Combined FE
	

